package com.krmes.mes;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.krmes.mes.app.AppContext;
import com.krmes.mes.app.AppManager;

import cn.hugo.android.scanner.CaptureActivity;

public class FragmentMe
        extends Fragment
        implements OnClickListener
{
    private TextView btn_about;
    private TextView btn_about2;
    private TextView btn_iqccheck;
    private TextView btn_pqccheck;
    private TextView btn_editpassword;
    private Button btn_exit;
    private ImageView productidscan;

    private TextView tv_userid;
    private TextView tv_username;
    private AppManager appManager = AppManager.getAppManager();


    private void initData()
    {
        this.tv_userid.setText("员工ID：" + AppContext.loginCode);
        this.tv_username.setText(AppContext.userName);
    }

    private void initView(View paramView)
    {
        this.tv_userid = ((TextView)paramView.findViewById(R.id.tv_userid));
        this.tv_username = ((TextView)paramView.findViewById(R.id.tv_username));
        this.btn_editpassword = ((TextView)paramView.findViewById(R.id.btn_editpassword));
        this.btn_editpassword.setOnClickListener(this);
        this.btn_iqccheck = ((TextView) paramView.findViewById(R.id.btn_iqccheck));
        this.btn_iqccheck.setOnClickListener(this);
        this.btn_pqccheck = ((TextView) paramView.findViewById(R.id.btn_pqccheck));
        this.btn_pqccheck.setOnClickListener(this);
        this.btn_about = ((TextView)paramView.findViewById(R.id.btn_about));
        this.btn_about.setOnClickListener(this);
        this.btn_about2 = ((TextView)paramView.findViewById(R.id.btn_about2));
        this.btn_about2.setOnClickListener(this);
        this.btn_exit = ((Button)paramView.findViewById(R.id.btn_exit));
        this.btn_exit.setOnClickListener(this);
        this.productidscan = ((ImageView)paramView.findViewById(R.id.productidscan));
        this.productidscan.setOnClickListener(this);
        this.tv_userid.setOnClickListener(this);
        this.tv_username.setOnClickListener(this);
    }

    public void prodscan( ){
        Intent intent;
        intent = new Intent(getContext(), CaptureActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//Intent.FLAG_RECEIVER_REGISTERED_ONLY
        intent.putExtra("fun",6);
        startActivity(intent);
    }

    public void onClick(View paramView)
    {
        Intent intent ;
        switch (paramView.getId())
        {
            case R.id.productidscan:
                prodscan();
                break;
            case R.id.tv_userid:
                prodscan();
                break;
            case R.id.tv_username:
                prodscan();
                break;
            case R.id.btn_exit:
                AppManager.getAppManager().AppExit(AppContext.getInstance());
                break;
            case R.id.btn_about2:
                //历史数据
                if ( AppContext.roleCode.contains("workman") ) {
                    intent = new Intent(getContext(), HistoryActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//Intent.FLAG_RECEIVER_REGISTERED_ONLY
                    startActivity(intent);
                }else{
                    AppContext.showToast("您没有有权限查看");
                }
                break;
            case R.id.btn_about:
                //AppContext.showToast("如有问题请联系管理员!");
                //FQC
                if ( AppContext.roleCode.contains("inspector") ) {
                    intent = new Intent(getContext(), CaptureActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//Intent.FLAG_RECEIVER_REGISTERED_ONLY
                    intent.putExtra("fun",5);
                    startActivity(intent);
                }else{
                    AppContext.showToast("您没有有权限查看");
                }
                break;
            case R.id.btn_iqccheck:
                //IQC
                if ( AppContext.roleCode.contains("inspector") ) {
                    intent = new Intent(getContext(), CaptureActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//Intent.FLAG_RECEIVER_REGISTERED_ONLY
                    intent.putExtra("fun", 3);
                    startActivity(intent);
                }else{
                    AppContext.showToast("您没有有权限查看");
                }
                break;
            case R.id.btn_pqccheck:
                //PQC
                if ( AppContext.roleCode.contains("instructor") ) {
                    intent = new Intent(getContext(), CaptureActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//Intent.FLAG_RECEIVER_REGISTERED_ONLY
                    intent.putExtra("fun",4);
                    startActivity(intent);
                }else{
                    AppContext.showToast("您没有有权限查看");
                }
                break;
            case R.id.btn_editpassword:
                //AppContext.showToast("如有问题请联系管理员!");
                intent = new Intent();
                intent.setClass(getContext(),ChangePwdActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//关掉所要到的界面中间的activity
                //intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);//设置不要刷新将要跳转的界面
                startActivity(intent);
                break;
            default:
                break;
        }

    }

    public View onCreateView(LayoutInflater paramLayoutInflater, ViewGroup paramViewGroup, Bundle paramBundle)
    {
        View localView = paramLayoutInflater.inflate(R.layout.fragment_me, paramViewGroup, false);
        initView(localView);
        initData();
        return localView;
    }
}
